package com.ldzy.webase.config;

import com.ldzy.webase.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @BelongsProject: spring-boot-demo
 * @BelongsPackage: com.ldzy.springbootdemo.config
 * @Author: wulangqi
 * @CreateTime: 2022-06-03  20:21
 * @Description: 拦截器配置，放行资源配置
 * @Version: 1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * @头像图片路径
     */
    @Value("${file.avatarImage.resource-path}")
    private String avatarImageResourcePath; // 头像,映射路径
    @Value("${file.avatarImage.upload-path}")
    private String avatarImageUploadPath; // 头像,上传路径

    /**
     * @首页资讯图片路径
     */
    @Value("${file.pressImage.resource-path}")
    private String pressImageResourcePath; // 首页资讯图片,映射路径
    @Value("${file.pressImage.upload-path}")
    private String pressImageUploadPath; // 首页资讯图片,上传路径

    /**
     * @轮播图图片路径
     */
    @Value("${file.slideshowImage.resource-path}")
    private String slideshowImageResourcePath; // 轮播图图片,映射路径
    @Value("${file.slideshowImage.upload-path}")
    private String slideshowImageUploadPath; // 轮播图图片,上传路径

    /**
     * @咨询分类封面图片路径
     */
    @Value("${file.consultImage.resource-path}")
    private String consultImageResourcePath; // 咨询分类封面图片,映射路径
    @Value("${file.consultImage.upload-path}")
    private String consultImageUploadPath; // 咨询分类封面图片,上传路径

    /**
     * @场馆预约封面图片路径
     */
    @Value("${file.sportsImage.resource-path}")
    private String sportsImageResourcePath; // 咨询分类封面图片,映射路径
    @Value("${file.sportsImage.upload-path}")
    private String sportsImageUploadPath; // 咨询分类封面图片,上传路径

    /**
     * @今日菜系图片路径
     */
    @Value("${file.todayCuisineImage.resource-path}")
    private String todayCuisineImageResourcePath; // 咨询分类封面图片,映射路径
    @Value("${file.todayCuisineImage.upload-path}")
    private String todayCuisineImageUploadPath; // 咨询分类封面图片,上传路径


    /**
     * @author ：Lq
     * @date ：2022/10/22 13:55
     * @description ：静态资源放行
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/" + avatarImageResourcePath + "**").addResourceLocations("file:" + avatarImageUploadPath);// 服务器中存放图片的路径
        registry.addResourceHandler("/" + pressImageResourcePath + "**").addResourceLocations("file:" + pressImageUploadPath);// 服务器中存放图片的路径
        registry.addResourceHandler("/" + slideshowImageResourcePath + "**").addResourceLocations("file:" + slideshowImageUploadPath);// 服务器中存放图片的路径
        registry.addResourceHandler("/" + consultImageResourcePath + "**").addResourceLocations("file:" + consultImageUploadPath);// 服务器中存放图片的路径
        registry.addResourceHandler("/" + sportsImageResourcePath + "**").addResourceLocations("file:" + sportsImageUploadPath);// 服务器中存放图片的路径
        registry.addResourceHandler("/" + todayCuisineImageResourcePath + "**").addResourceLocations("file:" + todayCuisineImageUploadPath);// 服务器中存放图片的路径
    }

    /**
     * @author ：Lq
     * @date ：2022/10/22 13:54
     * @description ：设置拦截哪些路径，不拦截哪些路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor())
                // 拦截规则 ，拦截那些路径
                .addPathPatterns("/**");
        // 添加白名单路径
//                .excludePathPatterns("/images/**", "/login", "/reg", "/reg/**","/product");
//                .excludePathPatterns("/user/api/login","/user/api/register");
    }

    /**
     * @author ：Lq
     * @date ：2022/10/22 13:55
     * @description ：全局注入拦截器并配置Bean
     */
    @Bean
    public AuthenticationInterceptor myInterceptor() {
        return new AuthenticationInterceptor();
    }
}

